<?php

class Raid_Model_RaidRow extends Pldkp_Db_Table_Row_Abstract
{
    public function validate()
    {
        if (!$this->raid_leader_user_id) {
            return false;
        }
        
        if(!$this->start_time) {
            return false;
        }
        if(!$this->end_time) {
            return false;
        }
        if(!$this->signup_deadline) {
            return false;
        }
        
        // Make sure signup < raid start < raid end
        $start = new Zend_Date($this->start_time);
        $end = new Zend_Date($this->end_time);
        $deadline = new Zend_Date($this->signup_deadline);
        
        if ($deadline->isLater($start) || $start->isLater($end)) {
            return false;
        }
        
        return true;
    }
    
    public function setDungeons(array $newDungeons)
    {
        $oldRaidDungeons = $this->findDependentRowset('Raid_Model_RaidDungeons');
        foreach ($oldRaidDungeons as $oldRaidDungeon) {
            echo "delete old: " . $oldRaidDungeon->raid_id . " / " . $oldRaidDungeon->dungeon_id . "<br/>";
            $oldRaidDungeon->delete();
        }
        
        foreach ($newDungeons as $newDungeon) {
            $raidDungeon = Raid_Model_RaidDungeons::getNew();
            $raidDungeon->raid_id = $this->id;
            $raidDungeon->dungeon_id = $newDungeon->id;
            $raidDungeon->save();
        }
    }
}